﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;

namespace QT.Windows
{
    public class NetWork
    {
        /// <summary>
        /// 判断给定IP是否能够Ping通
        /// </summary>
        /// <param name="ip">目标IP</param>
        /// <returns>bool</returns>
        public static bool ping(string ip)
        {
            if (IsIP(ip))
            {
                Ping p = new Ping();
                PingReply preply;
                try
                {
                    preply = p.Send(ip, 4000);
                }
                catch
                {
                    return false;
                }

                if (preply.Status == IPStatus.Success) return true;
                else return false;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断给定IP是否为合法IP
        /// </summary>
        /// <param name="ip">目标IP</param>
        /// <returns>bool</returns>
        public static bool IsIP(string ip)
        {
            try
            {
                IPAddress IP;
                bool Bool = IPAddress.TryParse(ip, out IP);
                return Bool;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 使用正则表达式判断IP是否为合法IP
        /// </summary>
        /// <param name="ip">待检查IP</param>
        /// <returns>bool</returns>
        public static bool CheckIP(string ip)
        {
            Regex rgx = new Regex(@"^(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.)((\d|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.){2}([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))$");
            if (!rgx.IsMatch(ip))
            {
                return false; ;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 判断给定IP是否为IPv6
        /// </summary>
        /// <param name="ip">目标IP</param>
        /// <returns>bool</returns>
        public static bool IsIPv4(string ip)
        {
            try
            {
                IPAddress IP = IPAddress.Parse(ip);
                if (IP.AddressFamily == AddressFamily.InterNetwork)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 判断给定IP是否为IPv4
        /// </summary>
        /// <param name="ip">目标IP</param>
        /// <returns>bool</returns>
        public static bool IsIPv6(string ip)
        {
            try
            {
                IPAddress IP = IPAddress.Parse(ip);
                if (IP.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// IP转换为long数值
        /// </summary>
        /// <param name="ip">待转换IP</param>
        /// <returns>long</returns>
        public static long ToInt(string ip)
        {
            string newIP="";
            string[] ips = ip.Split('.');
            for (int i = 0; i < ips.Length; i++)
            {
                switch (ips[i].Length)
                {
                    case 1:
                        ips[i] = "00" + ips[i];
                        break;
                    case 2:
                        ips[i] = "0" + ips[i];
                        break;
                    default:
                        break;
                }

                newIP += ips[i];
            }

            return Convert.ToInt64(newIP);
        }

        /// <summary>
        /// 对比两个IP，返回ip1与ip2的差值
        /// </summary>
        /// <param name="ip1">ip1</param>
        /// <param name="ip2">ip2</param>
        /// <returns>long</returns>
        public static long Compare(string ip1, string ip2)
        {
            return ToInt(ip1) - ToInt(ip2);
        }
    }
}
